import json

import jsonpath
import requests

data = {
    "accounts": "hu2374145",
    "pwd": "hu2374145",
    "type": "username"
}
headers = {
    "content-type": "application/json"
}
params = {
    "application": "app",
    "application_client_type": "weixin"
}

# 01.登录
url = "http://shop-xo.hctestedu.com/index.php?s=api/user/login"
res = requests.post(url=url, headers=headers, json=data, params=params)
print(res.text)
"""提取token的几种方式"""
# a.
token_list = jsonpath.jsonpath(res.json(),"$.data.token")
# b.
# token_list = jsonpath.jsonpath(json.loads(res.text),'$.data.token')
token = token_list[0]
print(token)

# 02.加入购物车api/cart/save
cart_url = "http://shop-xo.hctestedu.com/index.php?s=api/cart/save"
cart_data = {
    "goods_id": "10",
    "spec": "",
    "stock": 1
}
cart_headers = {
    "content-type": "application/json"
}
params = {
    "application": "app",
    "application_client_type": "weixin",
    "token": token
}
res = requests.post(url=cart_url, headers=cart_headers, json=cart_data, params=params)
print(res.text)

# 03.查看购物车api/cart/index
cart_index_url='http://shop-xo.hctestedu.com/index.php?s=api/cart/index'
params = {
    "application": "app",
    "application_client_type": "weixin",
    "token": token
}
res = requests.post(url=cart_index_url, params=params)
print(res.text)